package dk.android.houseenabler;

import java.math.BigInteger;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import dk.android.houseenabler.classes.FileHandle;
import dk.android.houseenabler.models.Answer;
import dk.android.houseenabler.models.AnswerCollection;
import dk.android.houseenabler.models.House;
import dk.android.houseenabler.models.HouseCollection;
import dk.android.houseenabler.models.Person;

public class SaveFileActivity extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file);
        
        //textWriteRead();
        
        //HouseCollection houses = new HouseCollection();
        
        House house = new House("Henning");
        //House house = new House("abcdefghijklmnopqrstuvwxyzzz");
        
        AnswerCollection ac = new AnswerCollection();
        
        Answer[] answersA = new Answer[33];
        Answer[] answersB = new Answer[49];
        Answer[] answersC = new Answer[100];
        Answer[] answersD = new Answer[6];
        
        answersA[0] = new Answer(true, "Blah1");
        answersA[1] = new Answer(true, "Blah2");
        answersA[2] = new Answer(true, "Blah3");
        answersA[3] = new Answer(true, "Blah4");
        answersA[4] = new Answer(true, "Blah5");
        
        answersB[0] = new Answer(true, "Blah6");
        answersB[1] = new Answer(true, "Blah7");
        
        answersC[0] = new Answer(false, "Blah6");
        answersC[1] = new Answer(true, "Blah7");

        answersA[3] = new Answer(false, "Blah4");
        answersA[4] = new Answer(false, "Blah5");
        
        answersD[4] = new Answer(true, "Blah6");
        answersD[5] = new Answer(true, "Blah7");
        
        ac.setAnswer('a', answersA);
        ac.setAnswer('b', answersB);
        ac.setAnswer('c', answersC);
        ac.setAnswer('d', answersD);
        
        house.setAnswers(ac);
        /*houses.addHouse(house);
        
        House house2 = new House("house");
        
        AnswerCollection ac2 = new AnswerCollection();
        
        Answer[] answersA2 = new Answer[33];
        Answer[] answersB2 = new Answer[49];
        Answer[] answersC2 = new Answer[100];
        Answer[] answersD2 = new Answer[6];
        
        answersA2[0] = new Answer(true, "Blah100");
        answersA2[1] = new Answer(false, "Blah200");
        answersA2[2] = new Answer(true, "Blah300");
        answersA2[3] = new Answer(true, "Blah400");
        answersA2[4] = new Answer(false, "Blah500");
        
        ac2.set('a', answersA2);
        ac2.set('b', answersB2);
        ac2.set('c', answersC2);
        ac2.set('d', answersD2);
        
        house2.setAnswers(ac2);
        houses.addHouse(house2);*/
        
        //textWriteRead(houses);
        exportEnvironmentProfile(house);

        //textToBinary(house);

	}
	
	public String toHex(String arg) {
	    return String.format("%x", new BigInteger(arg.getBytes()));
	}
	
	public byte[] hexStringToByteArray(String s) {
	    int len = s.length();
	    byte[] data = new byte[len / 2];
	    for (int i = 0; i < len; i += 2) {
	        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
	                             + Character.digit(s.charAt(i+1), 16));
	    }
	    return data;
	}
	
	public void exportEnvironmentProfile(House house) {
		int maxNameLength = 25;
		
		String title = house.getTitle();
		if(title.length() > maxNameLength) {
			title = title.substring(0, maxNameLength);
		}
		
		String lengthString = Integer.toHexString(title.length());
		if(lengthString.length() == 1) {
			lengthString = "0" + lengthString;
		}
		String theHex = lengthString;
		theHex += toHex(title);
		for(int i = title.length() ; i < maxNameLength ; i++) {
			theHex += "00";
		}
		
		char[] letters = {'a', 'b', 'c', 'd'};
		
		for(char c : letters) {
			for(Answer a : house.getAnswers().getAnswers(c)) {
				theHex += a != null ? (a.isChecked() ? "01" : "00") : "00";
			}
		}
		
		TextView tv = (TextView)findViewById(R.id.text);
        tv.setText(theHex);
		
		//String name = "abyz";
		
		byte[] b = hexStringToByteArray(theHex);
		FileHandle fh = new FileHandle(); 
		fh.writeDataToStorage("blah.mil", b);
		
		//(toHex("abyz"));
	}
	
	public void exportFunctionProfile(Person person) {
		int maxNameLength = 25;
		
		String title = person.getTitle();
		if(title.length() > maxNameLength) {
			title = title.substring(0, maxNameLength);
		}
		
		String lengthString = Integer.toHexString(title.length());
		if(lengthString.length() == 1) {
			lengthString = "0" + lengthString;
		}
		String theHex = lengthString;
		theHex += toHex(title);
		

		for(int i = title.length() ; i < maxNameLength ; i++) {
			theHex += "00";
		}
		
		for(Answer a : person.getAnswers()) {
			theHex += a != null ? (a.isChecked() ? "01" : "00") : "00";
			//theHex += Integer.toHexString(a != null ? (a.isChecked() ? 1 : 0) : 0); 
		}
		
		//TextView tv = (TextView)findViewById(R.id.text);
        //tv.setText(theHex);

		
		// 15 profiler (00 eller 01)
		for(Answer a : person.getAnswers()) {
			theHex += a != null ? (a.isChecked() ? "01" : "00") : "00";
		}
		

		theHex += "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000161601000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000";

		//byte[] b = hexStringToByteArray(theHex);
		//FileHandle fh = new FileHandle(); 
		//fh.writeDataToStorage("blah.mil", b);*/

		
		byte[] b = hexStringToByteArray(theHex);
		FileHandle fh = new FileHandle(); 
		fh.writeDataToStorage("fun.fun", b);

		//(toHex("abyz"));

	}
	
	public void textWriteRead(HouseCollection houses) {
		FileHandle fh = new FileHandle(); 
        
        TextView tv = (TextView)findViewById(R.id.text);
        tv.setText("Ikke noget endnu");
        
        /*AnswerCollection ac = new AnswerCollection();
       
        Answer[] answersA = new Answer[33];
        
        answersA[0] = new Answer(true, "Blah1");
        answersA[1] = new Answer(false, "Blah2");
        answersA[2] = new Answer(true, "Blah3");
        answersA[3] = new Answer(true, "Blah4");
        answersA[4] = new Answer(false, "Blah5");
        
        ac.set('a', answersA);*/
        System.out.println(houses.getHouses().get(0).getTitle());
        fh.writeObjectToStorage("answersObj.dat", houses);
        
        HouseCollection ac2 = (HouseCollection)fh.readObjectFromStorage("answersObj.dat");
        
        if(ac2 != null) {
        	AnswerCollection ac3 = (AnswerCollection)ac2.getHouses().get(0).getAnswers();
        	if(ac3 != null) {
		        Answer[] as = ac3.getAnswers('a');
		        Answer a = as[0];
		        Answer a2 = as[1];
		        
		        tv.setText(a.getNotes() + " " + a2.getNotes());
        	} else {
        		tv.setText("Er null");
        	}
        } else {
        	tv.setText("Er null2");
        }
	}
}
